<script setup>
import {
  getBizRubbishType,
  addBizRubbishType,
  updateBizRubbishType,
  listRubbishTypeTree,
  findMaxSort
} from "@/api/biz/bizRubbishType";
import storage from "@/utils/storage";
import { TOKEN } from "@/store/mutation-types";
const props = defineProps({
  id: {
    type: [Number, String],
    default: ""
  },
  busType: {
    type: [Number, String],
    default: ""
  }
});

const formRef = ref();
const fileHeahers = ref({
  url: import.meta.env.VITE_APP_API_BASEURL + "/common/upload",
  headers: {
    Authorization: "Bearer " + storage.local.get(TOKEN)
  }
});
const data = ref({
  loading: false,
  form: {
    id: props.id,
    status: 0,
    isIndex: 0
  },
  rules: {
    // deptCode: [{ required: true, message: "请输入分类编码", trigger: "blur" }],
    deptName: [{ required: true, message: "请输入分类名称", trigger: "blur" }]
  },
  deptTypeOptions: [],
  deptTreeList: []
});

onMounted(() => {
  if (data.value.form.id != "") {
    getInfo();
  } else {
    findMaxSort().then((res) => {
      data.value.form.treeSort = res.data;
    });
  }

  listRubbishTypeTree("", 3, props.busType).then((res) => {
    let menu = { id: "0", label: "主类目", children: [], key: 0 };
    data.value.deptTreeList = [];
    menu.children = getTreeData(res.data);
    data.value.deptTreeList.push(menu);
  });
});
function getTreeData(data) {
  return data.map((tree) => {
    tree.id = tree.id;
    tree.label = tree.title;
    if (tree.children) {
      tree.children = getTreeData(tree.children);
    }
    return tree;
  });
}
// 得到节点
function getnode(node, prop, event) {
  console.log("node", node, prop, event);
  data.value.form.parentId = node.id;
}
function handleAvatarSuccess(res, file) {
  console.log(file.response.data.url);
  data.value.form.zipCode = file.response.data.url;
}
function getInfo() {
  data.value.loading = true;
  getBizRubbishType(props.id).then((res) => {
    data.value.loading = false;
    data.value.form = res.data;
  });
}

defineExpose({
  submit(callback) {
    if (data.value.form.id == "") {
      formRef.value.validate((valid) => {
        if (valid) {
          data.value.form.busType = props.busType; // 洗护类型
          addBizRubbishType(data.value.form).then(() => {
            ElMessage.success({
              message: "保存成功",
              center: true
            });
            callback && callback();
          });
        }
      });
    } else {
      formRef.value.validate((valid) => {
        if (valid) {
          updateBizRubbishType(data.value.form).then(() => {
            ElMessage.success({
              message: "修改成功",
              center: true
            });
            callback && callback();
          });
        }
      });
    }
  }
});
</script>

<template>
  <div v-loading="data.loading">
    <el-form
      ref="formRef"
      :model="data.form"
      :rules="data.rules"
      label-width="120px"
      label-suffix=" "
      label-position="top"
      style="padding: 0 20px"
    >
      <el-row :gutter="30">
        <el-col :span="24">
          <el-form-item label="分类图标" prop="zipCode">
            <el-upload
              class="avatar-uploader"
              :action="fileHeahers.url"
              :headers="fileHeahers.headers"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
            >
              <img
                v-if="data.form.zipCode"
                :src="data.form.zipCode"
                class="avatar"
              />

              <svg-icon
                v-else
                class="el-icon-plus avatar-uploader-icon"
                name="ep:picture-rounded"
              />
            </el-upload>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="上级分类" prop="parentId">
            <el-tree-select
              v-model="data.form.parentId"
              :data="data.deptTreeList"
              :default-expand-all="true"
              node-key="id"
              @node-click="getnode"
              :check-strictly="true"
              style="width: 100%"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="分类名称" prop="deptName">
            <el-input
              v-model="data.form.deptName"
              placeholder="输入分类名称"
            ></el-input
          ></el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="分类编码" prop="deptCode">
            <el-input
              v-model="data.form.deptCode"
              placeholder="输入分类编码"
            ></el-input
          ></el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="状态" prop="status">
            <el-radio-group v-model="data.form.status">
              <el-radio-button label="0" checked="checked"
                >正常</el-radio-button
              >
              <el-radio-button label="1">停用</el-radio-button>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="首页显示" prop="isIndex">
            <el-radio-group v-model="data.form.isIndex">
              <el-radio-button label="0" checked="checked"
                >不显示</el-radio-button
              >
              <el-radio-button label="1">显示</el-radio-button>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="排序" prop="treeSort">
            <el-input v-model="data.form.treeSort"></el-input
          ></el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>
<style>
.avatar-uploader .el-upload {
  border: 2px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  width: 78px;
  height: 78px;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 38px;
  color: #409eff;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 58px;
  height: 58px;
  display: block;
}
</style>
